<template>
	<view class="news">
		<uni-section title="新闻分类" type="line" padding>
		 <u-subsection :list="categorylist" :current="current" @change="sectionChange"></u-subsection>
		 </uni-section>
		<mynew :newlist='newlist' @setgo='gotodetail'></mynew>
		<uni-pagination title="标题文字" prev-text="上一页" next-text="下一页"  @change="changepage"  :total="total" current="1"/>
	</view>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import {getnew,getcategory} from '../../../api/parkApi.js'
import {baseurl} from '../../../api/request.js'
import mynew from '../../../components/mynews/mynews'
import {fenlei,newws} from '@/mock/home.js'

const newlist =ref([])       //新闻列表
const pageNum= ref(1)        //当前页码
const pageSize=ref(10)       //每页数据条数
const total =ref()           //新闻总条数
const categorylist=ref([])  //新闻分类
const current=ref(0)       //当前新闻分类下标


//切换新闻分类
const sectionChange=async(e)=>{
  console.log(e);
	current.value=e
	if(e==0){
		getnewdata()
		return;
	}
	let newtype=categorylist.value[e].id

	let res = await getnew({
		data:{
			 pageNum:pageNum.value,
			 pageSize:pageSize.value,
			 type:newtype
		}
	})
	if(res.code==200){
		total.value=res.total
		 newlist.value=res.rows.map(item=>{
	
			item.img=(item.cover!=null)?(baseurl+item.cover):('../../../static/center4/comm.webp')
			return item	
			
		})	
	}else{
		//静态数据
		newlist.value=newws(e).rows
		total.value=newws(e).total

	}
	  console.log(newlist.value);
	
}


//点击去详细
const gotodetail=(val)=>{

	uni.navigateTo({
		url:`/pages/park/newde/newde?id=${val}`
	})
}
//点击下一页
const changepage=(e)=>{
	console.log(e);
	pageNum.value=e.current
	getnewdata()
}
//获取新闻列表
const getnewdata=async()=>{

         let res = await getnew({
	      data:{
		 pageNum:pageNum.value,
		 pageSize:pageSize.value
	  }
     })
    if(res.code==200){
	   total.value=res.total
	 newlist.value=res.rows.map(item=>{
		
		item.img=(item.cover!=null)?(baseurl+item.cover):('../../../static/center4/comm.webp')
		return item	
		
	  })	
     }else{
		 //静态数据
		total.value=newws(0).total
		newlist.value=newws(0).rows
		 
		 
	 }
    console.log(newlist.value);
 }
 
 //获取新闻分类
 const getcategorydate=async()=>{
	 let res= await getcategory({})

	 let frist =[{
		id: 0,
		name: "全部" 
	 }]
		 
	 if(res.code==200){
		 categorylist.value=frist.concat(res.data)
	 }else{
		 //静态数据
		 categorylist.value=fenlei()
	 }
	 
	 
	 
	 console.log(categorylist.value);
 }
 
 
 

onMounted(()=>{
	getnewdata();
	getcategorydate()
})

</script>

<style lang="scss">
.news{
	
 ::v-deep .uni-pagination{
	padding: 6px 14px;
 }	
}

</style>
